import { Button, Form, Input } from "antd";
import Layout from "./Layout";
import axios from 'axios'
import { API } from "../../config";
import { useHistory } from "react-router-dom";

export default function Signin() {
    const history = useHistory()
    const handleOnFinish = async value => {
        let response = await axios.post(`${API}/signin`, value)
        localStorage.setItem('token', JSON.stringify(response.data))
        const url = response.data.user.role === 1 ? '/admin/dashboard' : '/user/dashboard'
        history.push(url)
    }
    return (
        <Layout title='登录' subTitle=''>
            <Form onFinish={handleOnFinish}>
                <Form.Item label='邮箱' name='email'>
                    <Input />
                </Form.Item>
                <Form.Item label='密码' name='password'>
                    <Input.Password />
                </Form.Item>
                <Form.Item>
                    <Button type='primary' htmlType='submit'>登录</Button>
                </Form.Item>
            </Form>
        </Layout>
    )
}